from selenium.webdriver.common.by import By
from qturboframe.lib.webdriver.exceptions import SeleniumError
from qturboframe.public_ui.base_element import BaseElement
from qturboframe.lib.utils.CustomError.custom_error import CustomError


class BaseTable(BaseElement):

    def get_title_list(self):
        """
        获取hui表格的列名，hidden列除外，以列表的形式返回

        :Returns:
            返回表格列名集合

        :Raises:
            SeleniumError: 参数为非table元素
        """
        table_hidden_class = r'h-table-hidden'
        grid_hidden_class = r'h-editgird-hidden'
        gutter_class = r'gutter'
        if self._element.tag_name == 'table':
            title_list = list()
            tr = self._element.find_elements_by_attribute('xpath', './thead/tr', timeout=self.timeout)[-1]
            th_list = tr.find_elements_by_attribute('xpath', './th', timeout=self.timeout)
            for th in th_list:
                th_class = th.get_attribute('class').split(' ')
                if (table_hidden_class not in th_class and grid_hidden_class not in th_class and
                        gutter_class not in th_class):
                    th.focus()
                    text = th.text
                    title_list.append(text)
            return title_list
        else:
            message = '元素对象不为table对象，对象类型为： ' +self._element.tag_name
            raise SeleniumError(CustomError.Error_EQ03001004_InCorrect_Type, message)

    def get_tr_list(self):
        """
        获取hui表格的列名，以列表的形式返回

        :Returns:
            返回表格行集合

        :Raises:
            SeleniumError: 参数为非table元素
        """
        if self._element.tag_name == 'table':
            tr_list = self._element.find_elements_by_attribute('xpath', './tbody/tr', timeout=self.timeout)
            return tr_list
        else:
            message = '元素对象不为table对象，对象类型为： ' + self._element.tag_name
            raise SeleniumError(CustomError.Error_EQ03001004_InCorrect_Type, message)

    def get_text_list(self, columns=0, column=0):
        '''
        获取hui表格列文本集合

        :Args:
         - columns: 表格总列数
         - column: 所需获取列的列号

        :Returns:
            返回当前页的列文本集合

        :Raises:
            SeleniumError: 参数为非table元素
        '''
        if self._element.tag_name == 'table':
            body_list = list()
            td_list = self.get_cell_list()
            for i in range(0 ,len(td_list)):
                if columns:
                    if (i + 1) % columns == column % columns:
                        # td_list[i].focus()
                        text = td_list[i].text
                        # 在edit grid中文字有的会在input框中
                        if text == "":
                            input_element = td_list[i].exist("css selector", "input")
                            if input_element:
                                text = input_element.get_attribute("value")
                        body_list.append(text)
                else:
                    # td_list[i].focus()
                    text = td_list[i].text
                    # 在edit grid中文字有的会在input框中
                    if text == "":
                        input_element = td_list[i].exist("css selector", "input")
                        if input_element:
                            text = input_element.get_attribute("value")
                    body_list.append(text)
            return body_list
        else:
            message = '元素对象不为table对象，对象类型为： ' + self._element.tag_name
            raise SeleniumError(CustomError.Error_EQ03001004_InCorrect_Type, message)

    def get_cell_list(self, columns=0, column=0):
        """
        获取hui表格列单元格集合

        :Returns:
            返回当前页的列单元格集合

        :Raises:
            SeleniumError: 参数为非table元素
        """
        table_hidden_class = r'h-table-hidden'
        grid_hidden_class = r'h-editgird-hidden'
        if self._element.tag_name == 'table':
            body_list = list()
            td_list = self._element.find_elements_by_attribute(
                'xpath', './tbody/tr/td', timeout=self.timeout)
            for td in td_list:
                td_class = td.get_attribute('class').split(' ')

                #当一个单元格下面还有table时，则略过此单元格（即不获取此单元格的内容）
                if td.exist(By.CSS_SELECTOR, "table", timeout=0.1):
                    continue

                if table_hidden_class not in td_class and grid_hidden_class not in td_class:
                    body_list.append(td)
            cell_list = list()
            for i in range(0, len(body_list)):
                if columns:
                    if (i + 1) % columns == column % columns:
                        cell_list.append(body_list[i])
                else:
                    cell_list.append(body_list[i])
            return cell_list
        else:
            message = '元素对象不为table对象，对象类型为： ' + self._element.tag_name
            raise SeleniumError(CustomError.Error_EQ03001004_InCorrect_Type, message)

    def get_cell_list_in_row(self, row):
        '''

        :param row:
        :return:
        '''
        table_hidden_class = r'h-table-hidden'
        grid_hidden_class = r'h-editgird-hidden'
        if self._element.tag_name == 'table':
            cell_list = list()
            tr = self.get_tr_list()[row - 1]
            tr.focus()
            td_list = tr.find_elements_by_attribute('xpath', './td', timeout=self.timeout)
            for td in td_list:
                td_class = td.get_attribute('class').split(' ')
                if table_hidden_class not in td_class and grid_hidden_class not in td_class:
                    cell_list.append(td)
            return cell_list
        else:
            message = '元素对象不为table对象，对象类型为： ' + self._element.tag_name
            raise SeleniumError(CustomError.Error_EQ03001004_InCorrect_Type, message)
